<?php
/**
 * 后台商户管理模块
 * @author ezhu <ezhu@jipukeji.com>
 */

namespace Admin\Model;
use Think\Model;

class StoreModel extends Model{
    
    protected $tableName = 'store';
    
    protected $_validate = array(
            array('store_name','require','店铺名称必须填写！'),
            array('category_id','require','店铺分类必须选择！'),
            array('grade_id','require','店铺等级必须选择！')
    );
    
    
    /**
     * 获取商户详细信息
     * @param int $store_id
     */
    public function info($member_id){
        $map['member_id'] = $member_id;
        $map['status'] = array('gt',0);
        $field = 'ctime,atime';
        $data = M('store_apply')->where($map)->field($field,true)->find();
        //店铺经营类目
        $sell_class_name = unserialize($data['store_class_names']);
        $data['class_rate'] = explode(',',$data['store_class_rates']); 
        foreach ($sell_class_name as $key=>$val){
            $sell_class_name[$key] = explode(',',trim($val,','));
        }
        $data['sell_cate'] = $sell_class_name;
        //查询店铺的佣金比例
        $data['rate'] = M('Store')->where(array('store_id'=>$data['store_id']))->getField('rate');
        //查询店铺等级
        $data['grade_name'] = M('StoreGrade')->where(array('id='.$data['grade_id']))->getField('name');
        return $data;
    }
    
    public function update($data){
        $member_id=I('request.member_id');
        $start_time = I('post.business_licence_start');
        $end_time = I('post.business_licence_end');
        $data['business_licence_start'] = strtotime($start_time) ;
        $data['business_licence_end'] = strtotime($end_time) ;
       // dump($data);die;
        $res = M('store_apply')->where("member_id = $member_id")->save($data);
        return $res;
    }

    /**
     * 店铺分类，按照树分类来处理
     */
    public function category($field=true){
        $list = M('store_category')->field($field)->order('sort desc')->select();
        $list = list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_');
        return $list;
    }
    
    
    /**
     * 获取单个店铺信息，后台编辑需要
     * @param int $id
     */
    public function detail($id){
        $field = 'store_id,member_name,store_name,grade_id,category_id,status,end_time,rate';
        $map['store_id'] = intval($id);
        $data = M('Store')->where($map)->field($field)->find();
        $grade = M('store_grade')->field('id,name')->select();
        $rtnData['info']      = $data;
        $rtnData['grade']     = $grade;
        return $rtnData;
    }
    
    
    /**
     * 经营类目
     * @param array  $map    查询条件
     * @param string $field  字段
     * @param string $order  排序
     * @return $list
     */
    public function sellCategory($map=array(),$field='',$order='id desc'){
        $list = M('store_sell_category')->where($map)->field($field)->order($order)->select();
        return $list;
    }
    
    
    
    
}
